<!DOCTYPE html>
<html>
<head>
    <title>纯前端MC模组安装器</title>
    <meta charset="UTF-8">
    <!-- 引入JSZip用于打包文件 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        .mod-list { margin: 20px 0; border: 1px solid #ccc; padding: 10px; }
        .mod-item { display: flex; align-items: center; margin: 5px 0; }
        .mod-name { flex: 1; }
        button { margin: 0 5px; padding: 5px 10px; }
    </style>
</head>
<body>
    <h1>MC模组安装器</h1>
    
    <!-- 模组上传 -->
    <input type="file" id="modFile" accept=".jar" multiple>
    <button onclick="document.getElementById('modFile').click()">上传模组</button>
    <button onclick="exportMods()">导出全部模组</button>

    <!-- 模组列表 -->
    <div class="mod-list" id="modList"></div>

    <script>
        let db;
        const DB_NAME = 'MCModsDB';
        const STORE_NAME = 'mods';

        // 初始化IndexedDB
        const request = indexedDB.open(DB_NAME, 1);
        request.onupgradeneeded = (e) => {
            db = e.target.result;
            db.createObjectStore(STORE_NAME, { keyPath: 'id' });
        };
        request.onsuccess = (e) => {
            db = e.target.result;
            loadModList();
        };

        // 上传模组文件
        document.getElementById('modFile').addEventListener('change', async (e) => {
            const files = Array.from(e.target.files);
            for (const file of files) {
                const buffer = await file.arrayBuffer();
                const mod = {
                    id: Date.now() + '-' + file.name,
                    name: file.name,
                    data: new Uint8Array(buffer)
                };
                saveModToDB(mod);
            }
            loadModList();
        });

        // 存储模组到IndexedDB
        function saveModToDB(mod) {
            const tx = db.transaction(STORE_NAME, 'readwrite');
            tx.objectStore(STORE_NAME).put(mod);
        }

        // 加载模组列表
        async function loadModList() {
            const mods = await getAllMods();
            const list = document.getElementById('modList');
            list.innerHTML = mods.map(mod => `
                <div class="mod-item">
                    <span class="mod-name">${mod.name}</span>
                    <button onclick="deleteMod('${mod.id}')">删除</button>
                    <button onclick="downloadMod('${mod.id}')">导出</button>
                </div>
            `).join('');
        }

        // 获取所有模组
        function getAllMods() {
            return new Promise((resolve) => {
                const tx = db.transaction(STORE_NAME, 'readonly');
                const req = tx.objectStore(STORE_NAME).getAll();
                req.onsuccess = () => resolve(req.result || []);
            });
        }

        // 删除模组
        function deleteMod(id) {
            const tx = db.transaction(STORE_NAME, 'readwrite');
            tx.objectStore(STORE_NAME).delete(id);
            loadModList();
        }

        // 导出单个模组
        function downloadMod(id) {
            const tx = db.transaction(STORE_NAME, 'readonly');
            tx.objectStore(STORE_NAME).get(id).onsuccess = (e) => {
                const mod = e.target.result;
                const blob = new Blob([mod.data], { type: 'application/java-archive' });
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = mod.name;
                link.click();
            };
        }

        // 导出全部模组为ZIP
        async function exportMods() {
            const mods = await getAllMods();
            const zip = new JSZip();
            mods.forEach(mod => {
                zip.file(mod.name, mod.data);
            });
            zip.generateAsync({ type: 'blob' }).then(blob => {
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = 'mc-mods.zip';
                link.click();
            });
        }
    </script>
</body>
</html>